#include	"ScrollText.hpp"

ScrollText::ScrollText(std::list<std::string> listItems, unsigned int heigth, unsigned int width, WINDOW *parentWin)
  :_listItems(listItems), _heightWin(heigth), _widthWin(width), _parentWin(parentWin)
{
  this->_cursor = 0;
  this->_startDisplay = 0;
}

ScrollText::~ScrollText()
{
  
}

void	ScrollText::setListScrollText(std::list<std::string> listItems)
{
  this->_listItems = listItems;
}

void	ScrollText::cursorDown()
{
  if (this->_cursor < this->_listItems.size() - 1)
    this->_cursor += 1;
  if (this->_cursor - this->_startDisplay >= this->_heightWin - 2)
    this->_startDisplay += 1;
}

void	ScrollText::cursorUp()
{
  if (this->_startDisplay > 0 && this->_cursor == this->_startDisplay)
    this->_startDisplay -= 1;
  if (this->_cursor > 0)
    this->_cursor -= 1;
}

std::string	ScrollText::getLineOnCursor()
{
  unsigned int	lines = 0;
  std::list<std::string>::iterator	it;
  
  for (it = this->_listItems.begin(); it != this->_listItems.end(); it++)
    {
      if (lines == this->_cursor)
	return (*it);
      lines++;
    }
}

void	ScrollText::update()
{
  unsigned int	lines = 0;
  unsigned int	jump = 0;
  unsigned int	stringDeca = 0;
  std::string	strTmp;
  std::list<std::string>::iterator	it;

  for (it = this->_listItems.begin(); it != this->_listItems.end(); it++)
    {
      if (lines >= this->_startDisplay && lines - this->_startDisplay < this->_heightWin - 2)
	{
	  strTmp = (*it);
	  if (strTmp.length() > this->_widthWin - 2)
	    strTmp.resize(this->_widthWin - 2);
	  while (strTmp.length() < this->_widthWin - 2)
	    strTmp += " ";
	  if (lines == this->_cursor)
	    enableColorPair2(this->_parentWin);
	  mvwprintw(this->_parentWin, jump + 1, 1, "%s", strTmp.c_str());
	  jump++;
	  if (lines == this->_cursor)
	    disableColorPair2(this->_parentWin);
	}
      lines++;
    }
}

int		ScrollText::getStartDisplay()const
{
  return (this->_startDisplay);
}
